package com.iussoft.receiveData.service.plugins;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.tools.UUIDUtil;
import com.iussoft.model.DataParams;
import com.iussoft.receiveData.send.SendSearchManager;
import com.iussoft.tools.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @description: 同步发送
 * @author: Hou Dayu
 * @date: Created in 2020/3/12
 */
@Service("retrieve-data")
public class RetrieveDataPluginConnector extends SuperPluginconnector {

    @Autowired(required = false)
    @Qualifier("sendSearchManager")
    private SendSearchManager sendSearchManager;

    @Override
    public Object execute(ParamsVo vo) throws Exception {
        String dxpInterfaceInfo = vo.getParamsToStr("dxpInterfaceInfo");
        JSONObject data = JSON.parseObject(dxpInterfaceInfo);
        RequestMessage reqMsg =(RequestMessage)vo.getObj();
        ResultEntity entity = new ResultEntity();
        entity.setResult("true");
        JSONObject map = getInterfaceInfo(data.getString("fromServerId"));
        Object obj = null;
        if("1".equals(map.getString("ifLayOut"))){//是否是编排服务
            JSONArray arrays = (JSONArray)dataReceiveManager.execute(map,reqMsg,vo.getParamsToStr("traceId"),map.getString("exchangeMode"),"topic");
            if(arrays != null && !arrays.isEmpty()){
                for(int i=0;i<arrays.size();i++) {
                    JSONObject jsobj = arrays.getJSONObject(i);
                    String msg = jsobj.getString("sourceMsg");
                    if(StrUtil.isNull(msg)){
                        continue;
                    }
                    JSONObject jsons = new JSONObject();
                    jsons.put("ifTest",map.getString("ifTest"));
                    if(jsobj.containsKey("mergeParams")){
                        jsons.put("mergeParams",jsobj.get("mergeParams"));
                    }
                    if(jsobj.containsKey("defaultColumn")){//默认列
                        jsons.put("defaultColumn",jsobj.get("defaultColumn"));
                    }
                    obj = jsobj.getString("sourceMsg");
                }
            }
        } else {
            DataParams pvo = new DataParams();
            pvo.setParams("dataType","req");
            pvo.setParams("interfaceInfo", map.toJSONString());
            pvo.setParams("sourceMsg",reqMsg.getSourceMsg());
            pvo.setParams("requestUseIp",vo.getParamsToStr("requestUseIp"));
            pvo.setParams("requestCode",vo.getParamsToStr("requestCode"));
            pvo.setParams("infterfacePwd",reqMsg.getInfoByKey("requestPwd"));
            pvo.setParams("urlParams",reqMsg.getUrlMap() == null ? "" : JSON.toJSONString(reqMsg.getUrlMap()));
            pvo.setParams("headParams",JSON.toJSONString(reqMsg.getHeadMap()));
            String traceId = StrUtil.doNull(vo.getParamsToStr("traceId"), UUIDUtil.getUUID("",32));
            pvo.setParams("traceId", traceId);
            pvo.setTraceId(traceId);
            obj = sendSearchManager.sendDataMsg(pvo);
        }
        if(null != obj){
            entity.setEntity(obj);
                /*JSONObject json = JSON.parseObject(JSON.toJSONString(obj));
                if(UnityErrorCode.SUCCESSFUL.getValue().equals(json.get("errorCode"))) {
                    entity.setEntity(json.get("data"));
                }else{
                    entity.setMessage(UnityErrorCode.DATA_ERROR.getDesc());
                }*/
        } else {
            entity.setMessage("调用" + map.getString("interfaceUri") + "接口失败，错误信息为: 接口调用超时或未返回结果" );
            LoggerUtil.infoSource("exception = {}; infoType = exception; traceId = {}; interfaceCode = {}; requestTime = {}; errorMessage = {}; requestIp = {}; exeSys = receive; level = {}","Exception", vo.getParamsToStr("traceId"), map.getString("interfaceCode"), DateUtils.Now.fmt_yyyyMMdd_HHmmss(), entity.getMessage(),vo.getParamsToStr("requestUseIp"),"ERROR");
        }
        return entity;
    }

}
